package com.irdstudio.tdp.executor.core.dao;

import com.irdstudio.tdp.executor.core.dao.domain.SRoleuser;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户角色关系表类-[表名: 用户角色关系表(s_roleuser)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class SRoleuserDao {

	/* 连接对象 */
	Connection conn = null;	

	public SRoleuserDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertSRoleuser(SRoleuser sRoleuser) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into s_roleuser ( "
                + "roleno,actorno,state,orgid"
                + ") values ("+ "?,?,?,?" +")"
                );
            ps.setObject(1, sRoleuser.getRoleno());
            ps.setObject(2, sRoleuser.getActorno());
            ps.setObject(3, sRoleuser.getState());
            ps.setObject(4, sRoleuser.getOrgid());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert SRoleuser is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(SRoleuser sRoleuser) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from s_roleuser where 1=1"
                + " AND roleno = ? "
                + " AND actorno = ? "
                );
            ps.setObject(1, sRoleuser.getRoleno());
            ps.setObject(2, sRoleuser.getActorno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete SRoleuser is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(SRoleuser sRoleuser) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update s_roleuser set "
                + " roleno = ? ,"
                + " actorno = ? ,"
                + " state = ? ,"
                + " orgid = ? "
                + " where 1=1"
                + " AND roleno = ? "
                + " AND actorno = ? "
                );
            ps.setObject(1, sRoleuser.getRoleno());
            ps.setObject(2, sRoleuser.getActorno());
            ps.setObject(3, sRoleuser.getState());
            ps.setObject(4, sRoleuser.getOrgid());
            ps.setObject(5, sRoleuser.getRoleno());
            ps.setObject(5, sRoleuser.getActorno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update SRoleuser is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public SRoleuser queryByPk(SRoleuser sRoleuser) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        SRoleuser result = null;
        try {
            ps = conn.prepareStatement("select roleno,actorno,state,orgidfrom s_roleuser where 1=1 "
                + " AND roleno = ? "
                + " AND actorno = ? "
                );
            ps.setObject(1, sRoleuser.getRoleno());
            ps.setObject(2, sRoleuser.getActorno());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new SRoleuser();
				result.setRoleno(rs.getString("roleno"));
				result.setActorno(rs.getString("actorno"));
				result.setState(rs.getBigDecimal("state"));
				result.setOrgid(rs.getString("orgid"));
			}
        } catch (SQLException e) {
            throw new SQLException("update SRoleuser is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 sRoleuser 参数列表查询 不分页
	* @return int
	*/
    public List<SRoleuser> queryAll(SRoleuser sRoleuser) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<SRoleuser> result = new ArrayList<>();
        try {
            String sql = "select roleno,actorno,state,orgidfrom s_roleuser where 1=1 ";
                if (sRoleuser.getRoleno() != null) {
                    sql = sql + " AND roleno =  '" + sRoleuser.getRoleno() + "'";
                }
                if (sRoleuser.getActorno() != null) {
                    sql = sql + " AND actorno =  '" + sRoleuser.getActorno() + "'";
                }
                if (sRoleuser.getState() != null) {
                    sql = sql + " AND state =  '" + sRoleuser.getState() + "'";
                }
                if (sRoleuser.getOrgid() != null) {
                    sql = sql + " AND orgid =  '" + sRoleuser.getOrgid() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				SRoleuser obj = new SRoleuser();
				obj.setRoleno(rs.getString("roleno"));
				obj.setActorno(rs.getString("actorno"));
				obj.setState(rs.getBigDecimal("state"));
				obj.setOrgid(rs.getString("orgid"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update SRoleuser is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
